package com.yydream.common.utils;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yydream.dto.BasePageDTO;
import com.yydream.dto.Paging;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;


	public static <S,D extends Serializable>Paging<D> pageCopy(IPage<S>org, Class<D> clazz){
		Paging<D> dPaging = new Paging<D>(org.getTotal(), org.getSize(), org.getCurrent());
		List<D> ds = BeanUtil.copyToList(org.getRecords(), clazz);
		dPaging.setList(ds);
		return dPaging;
	}
	public static  <T> Page<T> page(BasePageDTO req) {
		return new Page<T>(req.getCurrPage(), req.getPageSize());
	}

	public static  <S, D> List<D> toList(List<S> list, Class<D> clazz) {
        return BeanUtil.copyToList(list, clazz);
	}

	public static <D extends Serializable>Paging<D> getCustomPageResult (List<D> dataList, int currPage, int pageSize) {
		int size = dataList.size();
		int startIndex = (currPage - 1) * pageSize;
		Paging<D> dPaging = new Paging<>(size, pageSize, currPage);
		if (size < startIndex) {
			return dPaging;
		}
		int min = Math.min(currPage * pageSize, size);
		dPaging.setList(dataList.subList(startIndex, min));
		return dPaging;
	}
	
}
